<?php

class Nuo_Html_Render_Div2
{
	public function render($group)
	{

		$tag = $group -> getMeta('tag', 'div');

		$xhtml = array();

		$formNoticeMsg = null;

		$notice = $group -> getNotice();

		if ($notice)
		{
			$formNoticeMsg = '<div class="formNotice">' . __text($notice) . '</div>';
		}

		$formErrorMsg = null;

		$errors = $group -> getErrors();

		if (!empty($errors))
		{
			$formErrorMsg = '<ul class="formElementErrors"><li>' . implode('</li><li>', $errors) . '</li></ul>';
		}

		$groupTitle = $group -> getMeta('title') ? ($group -> getNoTitle() ? '' : '<h1 class="formGroupTitle">' . $group -> getMeta('title') . '</div>') : '';

		$groupDesc = $group -> getMeta('desc') ? ($group -> getNoDesc() ? '' : '<div class="formGroupDesc">' . __text($group -> getMeta('desc')) . '</div>') : '';

		$footer = '';

		$xhtml = array();
		foreach ($group->getChildren() as $index => $children)
		{
			$xhtml1 = array();

			foreach ($children as $name => $ele)
			{
				$label = $ele -> getMeta('label') ? ($ele -> getNoTitle() ? '' : '<div class="formElementLabel">' . __text($ele -> getMeta('label')) . '</div>') : '';

				$desc = $ele -> getMeta('desc') ? ($ele -> getNoTitle() ? '' : '<div class="formElementDesc">' . __text($ele -> getMeta('desc')) . '</div>') : '';

				$errors = $ele -> getErrors();

				$errorMsg = null;

				if (!empty($errors))
				{
					$errorMsg = '<ul class="formElementErrors"><li>' . implode('</li><li>', $errors) . '</li></ul>';
				}

				$html = $ele -> render();
				$cls = $ele -> getWrapperClass();

				$xhtml1[] = '<div class="formElementWrap ' . $cls . '">' . $label . '<div class="formElementOuter">' . $desc . $errorMsg . $html . '</div>' . '</div>';

			}
			$xhtml[] = sprintf('<div class="formEleGroup">%s</div>', implode(PHP_EOL, $xhtml1));
		}
		return sprintf('<%s %s><div class="formGroupTitle">%s</div><div class="formGroupBd">%s%s%s%s</div>%s</%s>', $tag, $group -> __flat(), $groupTitle, $formNoticeMsg, $groupDesc, $formErrorMsg, implode(PHP_EOL, $xhtml), $footer, $tag);
	}

}
